from django.shortcuts import render,redirect,HttpResponse
from django.urls import reverse
from django.views import View
from .models import *

# Create your views here.

class Index(View):
    def get(self,request):
        data=news.objects.all()
        return render(request,'index.html',{'data':data})
class Deletnew(View):
    def get(self,request,id):
        news.objects.get(id=id).delete()
        return redirect(reverse('news:index'))

class Add(View):
    def get(self,request):
        data=new_fl.objects.all()
        return render(request,'add.html',{'data':data})
    def post(self,request):
        name=request.POST.get('name')
        nr=request.POST.get('nr')
        fl=request.POST.get('fl')
        news.objects.create(name=name,nr=nr,fl_id=fl)
        return redirect(reverse('news:index'))

class Updat(View):
    def get(self,request,u_id):
        data=news.objects.get(id=u_id)
        ca=new_fl.objects.all()
        return render(request,'update.html',{'data':data,'ca':ca})
    def post(self,request,u_id):
        name = request.POST.get('name')
        nr = request.POST.get('nr')
        fl = request.POST.get('fl')
        news.objects.filter(id=u_id).update(name=name,nr=nr,fl_id=fl)
        return redirect(reverse('news:index'))
